package com.example.mymvvmdemo.net.presenter;


import com.example.mymvvmdemo.MyApplication;
import com.example.mymvvmdemo.net.model.IModel;
import com.example.mymvvmdemo.net.model.PostBodyModel;
import com.example.mymvvmdemo.net.view.IView;
import com.example.mymvvmdemo.utils.LogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 只具有一个body参数的post请求统一presenter
 */
public class PostBodyPresenter implements IModel.PostBodyModel {

    private PostBodyModel postBodyModel = new PostBodyModel(this);

    private IView.PostBodyView postBodyView;

    public PostBodyPresenter(IView.PostBodyView postBodyView) {
        this.postBodyView = postBodyView;
    }

    public void startLoadPostBody(String url, HashMap<String, String> hashMap, String type) {
        postBodyModel.startLoadPostBody(url, MyApplication.getAuthToken(), hashMap, type);
    }

    public void startLoadPostBody(String url, HashMap<String, String> hashMap, RequestBody body, String type) {
        postBodyModel.startLoadPostBody(url, MyApplication.getAuthToken(), hashMap, body, type);
    }

    //回调给view
    @Override
    public void loadPostBodySuccess(String result, String type, int code) {
        LogUtils.d("load post body success type:" + type + ",result:" + result);
        LogUtils.printJson(result, type);
        postBodyView.loadPostBodySuccess(result, type, code);
    }

    @Override
    public void loadPostBodyFail(String message, String type) {
        LogUtils.d("load post body success type:" + type + ",message:" + message);
        postBodyView.loadPostBodyFail(message, type);
    }

    @Override
    public void loadPostBodyAccountError(int code) {
        LogUtils.d("load post body account error code :" + code);
        postBodyView.loadPostBodyAccountError(code);
    }

    @Override
    public void loadPostBodyNetWorkError(String message, String type) {
        LogUtils.d("load post body network error :" + message);
        postBodyView.loadPostBodyNetWorkError(type);
    }

}
